// pages/distribution/distribution.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data_url: "",
    img_url: "",
    buyCar2: [],
    iv: "",
    userInfo: {},
    encryptedData: "",
    long: 0,
    lat: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    if (options.sharekey) {
      app.globalData.fatherid = options.sharekey || app.globalData.sharekey
    }
    var that = this
    this.setData({
      data_url: app.globalData.data_url
    })
    //确定地理位置
    wx.getLocation({
      success: function (res) {
        that.setData({
          long: res.longitude,
          lat: res.latitude
        })
      },
    })
    //确定用户
    wx.login({
      success: function (res) {
        that.setData({
          js_code: res.code
        })
        wx.request({
          url: that.data.data_url + "/GPD/wxlogin.do",
          data: {
            js_code: res.code
          },
          success: function (res) {
            app.globalData.sessionId = res.data.session_id
            wx.hideLoading()
            wx.getUserInfo({
              success: function (res) {
                app.globalData.nickName = res.userInfo.nickName
                that.setData({
                  userInfo: res.userInfo,
                  iv: res.iv,
                  encryptedData: res.encryptedData
                })
                app.getSessionId().then(function(){
                  wx.request({
                    url: that.data.data_url + '/GPD/WXUserInfo.do',
                    data: {
                      iv: res.iv,
                      encryptedData: res.encryptedData,
                      session_id: app.globalData.sessionId,
                      longitude: that.data.long,
                      latitude: that.data.lat,
                    },
                    success: function (res) {
                      wx.hideLoading()
                      app.globalData.sharekey = res.data.sharekey
                    }
                  })
                })
              },
              fail: function (err) {
                wx.showToast({
                  title: '请先授权用户信息',
                })
                setTimeout(function () {
                  wx.openSetting({
                    success:function(res){
                      wx.navigateBack({
                        delta:1
                      })
                    }
                  })
                }, 1500)
              }
            })
          }
        })
      },
      fail:function(res){
        console.log(res)
      }
    })
    wx.request({
      url: that.data.data_url + '/GPD/getAgentGoods.do',
      success: function (res) {
        var obj1 = new Object;
        var arr = new Array;
        obj1.num = 1,
          obj1.title = res.data.agentcommodity.commodityname,
          obj1.id = res.data.agentcommodity.id,
          obj1.price = res.data.agentcommodity.price,
          obj1.image = res.data.agentcommodity.imgurl[0],
          obj1.enableInterPay = res.data.agentcommodity.integralpurchase,//是否支持积分支付
          obj1.selected = true;
        arr[0] = obj1
        that.setData({
          buyCar2: arr
        })
        app.buyCar2 = that.data.buyCar2
      }
    })
    wx.request({
      url: that.data.data_url + '/GPD/getCars.do',
      data: {
        type: 2
      },
      success: function (res) {
        wx.hideLoading()
        that.setData({
          img_url: res.data[0].carouselFigureurl
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  jumpOrders: function () {
    wx.navigateTo({
      url: '/pages/orders/orders?isAgent=true',
    })
  },
  onShareAppMessage: function (res) {
    app.getSessionId().then(function () {
      return {
        title: app.globalData.nickName + '向您推荐果霖鲜',
        path: '/pages/distribution/distribution'
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})